Skip to content

fix: Add a fallback matching mechanism for output_files#1237

Open
alopezz wants to merge 2 commits intobazel-contrib:mainfrom
alopezz:improve-output-files
Open

fix: Add a fallback matching mechanism for output_files#1237
alopezz wants to merge 2 commits intobazel-contrib:mainfrom
alopezz:improve-output-files

Conversation

@alopezz
Copy link

@alopezz alopezz commented Jan 27, 2026

This PR proposes a fallback mechanism for the output_files rule, to make it more usable with external repositories.

Specifically, I was trying to select files from the DefaultInfo returned by a rule implemented on a build file applied to an http_archive source and failed until I discovered that the short_path for these files is a relative path containing the canonical repo name, for example:

❯ bazel cquery @openssl//:openssl --output=starlark --starlark:expr='[f.short_path for f in providers(target)["DefaultInfo"].files.to_list()]'
["../+_repo_rules+openssl/openssl/include", "../+_repo_rules+openssl/openssl/lib/pkgconfig", "../+_repo_rules+openssl/openssl/bin/openssl", "../+_repo_rules+openssl/openssl/lib/libssl.so", "../+_repo_rules+openssl/openssl/lib/libcrypto.so"]

The documentation for output_files references select_file from skylib, but the implementation of that rule seems to just match based on the ending (endswith), which is much more relaxed but also more convenient to use.

This attempts a middle ground, where it keeps the existing mechanism based on short_path (ensuring backwards compatibility), while adding a fallback mechanism that reconstructs the execution path such that it can match to the path; this works at least for the specific case that I was working with.

@alopezz alopezz changed the title Add a fallback matching mechanism for output_files fix: Add a fallback matching mechanism for output_files Jan 27, 2026
@aspect-workflows
Copy link

aspect-workflows bot commented Jan 27, 2026

Bazel 8 (Test)

2 test targets passed

Targets
//lib/tests/copy_to_directory:case_22_test [k8-fastbuild]                    46ms
//lib/tests/copy_to_directory_bin_action:test [k8-fastbuild]                 77ms

Total test execution time was 123ms. 134 tests (98.5%) were fully cached saving 16s.


Bazel 9 (Test)

2 test targets passed

Targets
//lib/tests/copy_to_directory:case_22_test [k8-fastbuild]                    313ms
//lib/tests/copy_to_directory_bin_action:test [k8-fastbuild]                 65ms

Total test execution time was 378ms. 134 tests (98.5%) were fully cached saving 18s.


Bazel 7 (Test)

e2e/api_entries

All tests were cache hits

1 test (100.0%) was fully cached saving 24ms.


Bazel 8 (Test)

e2e/api_entries

All tests were cache hits

1 test (100.0%) was fully cached saving 30ms.


Bazel 9 (Test)

e2e/api_entries

All tests were cache hits

1 test (100.0%) was fully cached saving 50ms.


Bazel 7 (Test)

e2e/copy_action

All tests were cache hits

1 test (100.0%) was fully cached saving 31ms.


Bazel 8 (Test)

e2e/copy_action

All tests were cache hits

1 test (100.0%) was fully cached saving 26ms.


Bazel 9 (Test)

e2e/copy_action

All tests were cache hits

1 test (100.0%) was fully cached saving 32ms.


Bazel 7 (Test)

e2e/copy_to_directory

All tests were cache hits

6 tests (100.0%) were fully cached saving 297ms.


Bazel 8 (Test)

e2e/copy_to_directory

All tests were cache hits

6 tests (100.0%) were fully cached saving 286ms.


Bazel 9 (Test)

e2e/copy_to_directory

All tests were cache hits

6 tests (100.0%) were fully cached saving 349ms.


Bazel 7 (Test)

e2e/coreutils

All tests were cache hits

4 tests (100.0%) were fully cached saving 247ms.


Bazel 8 (Test)

e2e/coreutils

All tests were cache hits

4 tests (100.0%) were fully cached saving 216ms.


Bazel 9 (Test)

e2e/coreutils

All tests were cache hits

4 tests (100.0%) were fully cached saving 198ms.


Bazel 7 (Test)

e2e/external_copy_to_directory

All tests were cache hits

1 test (100.0%) was fully cached saving 31ms.


Bazel 8 (Test)

e2e/external_copy_to_directory

All tests were cache hits

1 test (100.0%) was fully cached saving 26ms.


Bazel 9 (Test)

e2e/external_copy_to_directory

All tests were cache hits

1 test (100.0%) was fully cached saving 32ms.


Bazel 7 (Test)

e2e/smoke

All tests were cache hits

4 tests (100.0%) were fully cached saving 611ms.


Bazel 8 (Test)

e2e/smoke

All tests were cache hits

4 tests (100.0%) were fully cached saving 508ms.


Bazel 9 (Test)

e2e/smoke

All tests were cache hits

4 tests (100.0%) were fully cached saving 618ms.


Bazel 7 (Test)

e2e/write_source_files

All tests were cache hits

1 test (100.0%) was fully cached saving 39ms.


Bazel 8 (Test)

e2e/write_source_files

All tests were cache hits

1 test (100.0%) was fully cached saving 33ms.


Bazel 9 (Test)

e2e/write_source_files

All tests were cache hits

1 test (100.0%) was fully cached saving 40ms.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7d024a28af

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines 103 to 105
for file in files_list:
if file.path == "/".join([ctx.bin_dir.path, ctx.attr.target.label.workspace_root, path]):
return file

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Match external source files, not just bin outputs

The new fallback reconstructs a path under ctx.bin_dir.path, then compares it to file.path. That only works for generated outputs in the bin tree. For external repos that expose source files via DefaultInfo (e.g., filegroup(srcs = glob(...)) in an http_archive), file.path is under external/<repo>/... with no bazel-out/.../bin prefix, so this fallback still never matches and output_files continues to fail for the common “external repo sources” case the change is targeting. You likely need an additional check against the workspace-root execution path (or other roots) so source files can be found too.

Useful? React with 👍 / 👎.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's indeed true (I've manually confirmed). Even though it looks like a less likely use case, I see no reason not to cover it as well, I fixed it on a second commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant